package com.teamwork.module03.service.file.impl;

import com.teamwork.module03.controller.UserIconController;
import com.teamwork.module03.util.FileUtils;
import com.teamwork.module03.service.file.UploadService;
import com.teamwork.common.entity.common.UserIcon;
import com.teamwork.common.result.Result;
import com.teamwork.module03.service.ArticleService;
import com.teamwork.module03.service.AuthorService;
import com.teamwork.module03.service.UserIconService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@Service
@Slf4j
public class UploadServiceImpl implements UploadService {

    @Value("${user.icon}")
    private String userIconPath;
    @Value("${upload.basePath}")
    private String coverPath;
    //文章内容上传路径
    @Value("${txt.path}")
    private String contentPath;
    @Value("${article.imgPath}")
    private String imgPath;
    @Resource
    private UserIconService userIconService;
    @Resource
    private ArticleService articleService;
    @Resource
    private AuthorService authorService;

    @Override
    public Result uploadUserIcon(MultipartFile file, Integer userid) throws IOException {

        String prefix = "usericon-";

       String finalName = FileUtils.uploadByUserId(userid,file,userIconPath,prefix);


        //将用户数据库的图片途径改了
        UserIcon icon = userIconService.getByUserId(userid);
        String iconprefix = "http://localhost:8081/img/user/icon/";

        if(icon==null){
            //表示新增用户图片
            UserIcon userIcon = new UserIcon();
            userIcon.setUserId(Long.valueOf(userid));
            userIcon.setIcon(iconprefix + finalName);
            boolean b = userIconService.save(userIcon);
           if(!b){
               log.error("新增用户头像失败!用户id:{}",userid);
               return Result.failure("新增用户头像失败！");
           }
            return Result.success();
        }

        icon.setIcon(iconprefix + finalName);
        boolean b = userIconService.updateById(icon);
        if(!b){
            log.error("用户id:{}上传图像修改数据库错误",userid);
            return Result.failure("上传失败！，后台错误");
        }

        return Result.success("图片上传成功！",iconprefix+finalName);
    }

    /**
     * 将文章的内容存入磁盘
     * @param articleId 文章id
     * @param userid 用户id
     * @param content 文章内容
     * @return 返回的是文件的最近路径
     */
    @Override
    public String uploadContent(Long articleId, Integer userid, String content) throws IOException {
        //前缀
        String prefix = "article-"+userid+"-"+articleId + "-";

        String finalName = FileUtils.uploadTxt( content, contentPath, prefix);

        String finalPath = "arts/"+finalName;


        return finalPath;
    }

    /**
     * 上传文章封面
     * @param articleId 文章id
     * @param userid 用户id
     * @param cover 文章封面
     * @return 最终路径
     */
    @Override
    public String uploadCover(Long articleId, Integer userid, MultipartFile cover) throws IOException {
        String prefix = "cover-"+userid + "-" + articleId + "-";

        String finalName = FileUtils.uploadByUserId(userid, cover, coverPath, prefix);

        String finalPath = "http://localhost:8081/img/arts/" + finalName;
        return finalPath;

    }

    /**
     * 上传文章内容的图片
     * @param file 内容图片
     * @return
     */
    @Override
    public Result uploadContentImg(MultipartFile file) throws IOException {

        String prefix = "content-";

        String finalName = FileUtils.uploadContnetImg(file,imgPath,prefix);

        String finalPath = "http://localhost:8081/img/content/" + finalName;
        return Result.success("上传成功！",finalPath);

    }

    /**
     * 设置默认用户图片
     * @param id
     */
    @Override
    public void setMorenUserIcon(Long id) {
        UserIcon userIcon = new UserIcon();
        userIcon.setUserId(id);
        //将用户数据库的图片途径改了
        String icon = "http://localhost:8081/img/user/icon/001.jpg";
        userIcon.setIcon(icon);
        boolean b = userIconService.save(userIcon);
        if(!b){
            log.error("用户id:{}设置默认图像错误",id);
        }
    }


}
